/**
 * 作弊过滤
 * @author Peter
 *
 */
angular.module('imsApp').controller('cheatRtController', ['$scope', '$rootScope', '$state','$filter', 'cheatRtsManager',
    'FocusManager','DisallowManager','$sce','FileManager','ImsDialog',
    function ($scope, $rootScope, $state, $filter,cheatRtsManager,FocusManager,DisallowManager,$sce,FileManager,ImsDialog) {
    //Search data
    $scope.search = {method:$state.params.method||'cuid',
        startDay: $state.params.startDay || null,
        endDay: $state.params.endDay || null,
        status:$state.params.status,type:$state.params.type,pageNo:$state.params.pageNo || 1,pageSize:$state.params.pageSize||10};

    //Init
    $scope.status=[500,501,502];
    $scope.types= $scope.search.method=='ip'?[401,402]:[403,404];
    //Page
    $scope.paginationConf = {
        currentPage: $scope.search.pageNo,
        itemsPerPage: $scope.search.pageSize,
        pagesLength: 9,
        search: false,
        onChange: function(){
            $scope.search.pageNo = this.currentPage;
            $scope.search.pageSize = this.itemsPerPage;
            $state.go($state.current.name, $scope.search);
        }
    };

    //搜索公共部分
    var searchGo = function(){
        $scope.search.pageNo = 1;
        $scope.search.pageSize = 10;
        $state.go($state.current.name,$scope.search);//第一个参数是当前路由，第二个参数是传的参数
    };

    //Search
    $scope.searchClick = function(){
        searchGo();
    };

    //Choose date
    $scope.rangeDate = {
        startDate:$scope.search.startDay ? moment($scope.search.startDay).format('X'):null,
        endDate:$scope.search.endDay? moment($scope.search.endDay).format('X'):null,
        onSelected: function (data, scope) {  //submit
            $scope.search.startDay =data.startDate? moment(new Date(parseFloat(data.startDate) * 1000)).format("YYYY-MM-DD"):null;
            $scope.search.endDay = data.endDate?moment(new Date(new Date(parseFloat(data.endDate) * 1000))).format("YYYY-MM-DD"):null;
            searchGo();

        }
    };


    //Get data
    if($scope.search.method =='ip'){
        //Get IP list
        cheatRtsManager.cheatRtsIps([$scope.search.pageNo,$scope.search.pageSize],$scope.search).then(function (result) {
            $scope.lists = result.data.pageList;
            $scope.paginationConf.totalItems = result.data.total;
        })
    }else{
        //Get ID list
        cheatRtsManager.cheatRtsCuids([$scope.search.pageNo,$scope.search.pageSize],$scope.search).then(function (result) {
            $scope.lists = result.data.pageList;
            $scope.paginationConf.totalItems = result.data.total;
        })
    }

    //初始化全选值为false
        $scope.checkData={allChecked:false}

    //全选
   $scope.selectAllChange=function (checked) {
       $scope.lists.forEach(function (item) {
           item.checked=checked;
       })
   }

   //选择一个
   $scope.selectOneChange=function (checked) {
       if(!checked){
           $scope.checkData.allChecked=false;
       }else {
           ($filter('filter')($scope.lists,{checked:true}).length)==$scope.lists.length?$scope.checkData.allChecked=true:$scope.checkData.allChecked=false;
       }
   }

        /**
         * Add Focus
         * @param item
         */
     $scope.addFocus = function(item){
         if($scope.search.method=='ip'){
             FocusManager.ipAdd({list:[[item.ip,item.type]]}).then(function (result) {
                 if (result.data.c == "s") {
                     $state.reload();
                 }
             })
         }else{
             FocusManager.cuidAdd({list:[[item.cuid,item.type]]}).then(function (result) {
                 if (result.data.c == "s") {
                     $state.reload();
                 }
             })
         }
     };

        /**
         * Add Disallow
         * @param item
         */
    $scope.addDisallow = function(item){
        if($scope.search.method=='ip'){
            DisallowManager.ipAdd({list:[[item.ip,item.type]]}).then(function (result) {
                if (result.data.c == "s") {
                    $state.reload();
                }
            })
        }else{
            FocusManager.cuidAdd({list:[[item.cuid,item.type]]}).then(function (result) {
                if (result.data.c == "s") {
                    $state.reload();
                }
            })
        }
    };

        /**
         * Choose IP or ID
         * @param val
         */
     $scope.filterClick = function(val){
         $scope.search.method = val;
         $scope.search.status=0;
         $scope.search.type=0;
         searchGo();

     }

        /**
         * Forbide all (IP/cuid)
         */
     $scope.ForbideAll = function(){
         var tempArray = [];

         $scope.lists.forEach(function(item){
             if(item.checked==true){
                 if(item.status != 501)
                     tempArray.push([item.ip?item.ip:item.cuid,item.type]);
             }
         });
         if(tempArray.length>0){
             if($scope.search.method=='ip'){
                 DisallowManager.ipAdd({list:tempArray}).then(function (result) {
                     if(result.data.c=='s'){
                         $state.reload();
                     }
                 });
             }else{
                 DisallowManager.cuidAdd({list:tempArray}).then(function (result) {
                     if(result.data.c=='s'){
                         $state.reload();
                     }
                 });
             }
         }else{
             ImsDialog.open({title:"作弊过滤",message:"没有可封禁的数据。"}).then(function (a) {
             })

         }

     };


        /**
         * Focus all (IP/cuid)
         *
         */
     $scope.FocusAll = function(){
         var tempArray = [];
         var disAllowNum = 0;
         var disAllowArray = [];
         $scope.lists.forEach(function(item){
             if(item.checked==true){
                 if(item.status==501){
                     disAllowArray.push(item);
                     disAllowNum++;
                 }else if(item.status==502){
                     tempArray.push([item.ip?item.ip:item.cuid,item.type]);
                 }

             }
         });
         if(tempArray.length>0){
             if($scope.search.method=='ip'){
                 FocusManager.ipAdd({list:tempArray}).then(function (result) {
                     if(result.data.c=='s'){
                         if(disAllowNum){
                             ImsDialog.open({title:"作弊过滤",message:"您有"+disAllowNum+"条数据关注失败（原因：已封禁的数据不能关注）"}).then(function (a) {
                             })
                         }
                         $state.reload();
                     }
                 });
             }else{
                 FocusManager.cuidAdd({list:tempArray}).then(function (result) {
                     if(result.data.c=='s'){
                         if(disAllowNum){
                             ImsDialog.open({title:"作弊过滤",message:"您有"+disAllowNum+"条数据关注失败（原因：已封禁的数据不能关注）"}).then(function (a) {
                             })
                         }
                         $state.reload();
                     }
                 });
             }
         }else{
             ImsDialog.open({title:"作弊过滤",message:"没有可关注的数据。"}).then(function (a) {
             })
         }

     };
 /*    //组装文件
        var fileBody={
            alias:"photoFile",//后台接收字段
            url:'/ims/adContracts/image',//接口
            maxSize:20,//单个文件尺寸限制
            maxItem:20,//上传文件个数限制
            type:['Jpg','jpeg','png','mP3','Mp4']//上传文件类型
        };
        //获得文件
        $scope.uploaderImage = FileManager.getFile(fileBody);


        $scope.submitImage = function () {
           var u = FileManager.uploadFile($scope.urlList,$scope.uploaderImage,true)
            console.log(u+"=----")
        }*/

}]);

